package com.igit.contact.action;

import java.io.PrintWriter;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import com.gosophia.organizationManagement.dto.OrganizationTreeDTO;
import com.gosophia.organizationManagement.ws.OrganizationManagementWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophiaigit.common.busi.service.IGroupUtilService;
import com.gosophiaigit.contact.busi.entity.ContactGroup;
import com.gosophiaigit.contact.busi.service.IContactGroupService;
import com.gosophiaigit.contact.busi.service.IContactService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 联系人组操作的action
 * @author guodp
 *
 */
@SuppressWarnings("unchecked")
@Controller("contactGroupAction")
@Scope("prototype")
public class ContactGroupAction extends ActionSupport{

	private static final long serialVersionUID = 1L;

	@Autowired
	private IContactGroupService contactGroupService;
	@Autowired
    private OrganizationManagementWebService organizationManagementWebService;
	@Autowired
	private IGroupUtilService groupUtilService;
	@Autowired
	private IContactService contactService;
	/** 组ID */
	private Long groupId;
	
	/**组实体 */
	private ContactGroup contactGroup;
	
	private String groupName;
	
	/** 组类型 */
	private Long groupType;
	
	/** 所有联系人个数 */
	private int allContactNumber;
	
	/** 最近联系人个数*/
	private int recentContactNumber;
	
	private List list;
	
	private boolean flag;
	
    private String organizationDTOs;
    private Long orgId;
	
    /**
     * 初始化组织机构树
     * @return
     */
	public String initContact(){
		List<OrganizationTreeDTO> organizationDTOList = organizationManagementWebService.findRootOrganizations();
        organizationDTOs = JSONArray.fromObject(organizationDTOList).toString().replace("\"","\\\"" );
        return SUCCESS;
	}
	
	/**
	 * 查询组列表
	 * @return
	 */
	public String findGroupList(){
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
			.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
			.getUserInfoId();
		Long userAccountId = userDetails.getUserAccountDTO().getUserAccountId();
		list = contactGroupService.findGroupListByGroupType(userInfoId,userAccountId,groupType);
		allContactNumber = contactGroupService.findContactList(userAccountId).size();
		recentContactNumber = contactGroupService.findContactRecordCountList(userInfoId,userAccountId).size();
		return SUCCESS;
	}
	
	/**
	 * 查询组下用户列表
	 * @return
	 */
	public String findContactByContactGroupId(){
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
			.getContext().getAuthentication().getPrincipal();
		Long userAccountId = userDetails.getUserAccountDTO().getUserAccountId();
		
		list = contactGroupService.findContactByContactGroupId(groupId,userAccountId);
		return SUCCESS;
	}
	
	/**
	 * 查询所有联系人
	 * @return
	 */
	public String findAllContact(){
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
			.getContext().getAuthentication().getPrincipal();
		Long userAccountId = userDetails.getUserAccountDTO().getUserAccountId();
		
		list = contactGroupService.findContactList(userAccountId);
		return SUCCESS;
	}
	
	/**
	 * 查询最近联系人
	 * @return
	 */
	public String findRecentContact(){
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
			.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
			.getUserInfoId();
		Long userAccountId = userDetails.getUserAccountDTO().getUserAccountId();
		
		list = contactGroupService.findContactRecordCountList(userInfoId,userAccountId);
		return SUCCESS;
	}
	
	/**
	 * 添加组
	 * @return
	 */
	public String savaContactGroup(){
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out;
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
			.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
			.getUserInfoId();
		
		ContactGroup cGroup = new ContactGroup();
		if(groupType == ContactGroup.PUBLIC_GROUP){
			flag = contactGroupService.exitPublicGroupName(null, groupName);
		}else{
			Long contactId = contactService.findContactByUserInfoId(userInfoId).getContactId();
			cGroup.setGroupOwner(contactId);
			flag = contactGroupService.exitPrivateGroupName(null, groupName, contactId);
		}
		if(flag){
			try{
				out = response.getWriter();
				out.write(flag+"");
				response.flushBuffer();
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			cGroup.setGroupName(groupName);
			cGroup.setGroupType(groupType);
			contactGroupService.addContactGroup(cGroup);
		}
		return null;
	}
	
	/**
	 * 删除组
	 * @return
	 */
	public String delContactGroup(){
		HttpServletResponse response = ServletActionContext.getResponse();
		flag = contactGroupService.deleteContactGroup(groupId);
		PrintWriter out;
		try {
			out = response.getWriter();
			out.write(flag+"");
			response.flushBuffer();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 修改组
	 * @return
	 */
	public String updateContactGroup(){
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out;
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
			.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
			.getUserInfoId();
		if(groupType == 1239){
			flag = contactGroupService.exitPublicGroupName(groupId, groupName);
		}else{
			Long contactId = contactService.findContactByUserInfoId(userInfoId).getContactId();
			flag = contactGroupService.exitPrivateGroupName(groupId, groupName, contactId);
		}
		if(flag){
			try{
				out = response.getWriter();
				out.write(flag+"");
				response.flushBuffer();
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			ContactGroup cGroup = contactGroupService.findContactGroupById(groupId);
			cGroup.setGroupName(groupName);
			contactGroupService.updateContactGroup(cGroup);
		}
		return null;
	}
	
	/**
	 * 得到机构下的所有人员
	 * @return
	 */
	public String findContactByOrgId(){
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
		.getContext().getAuthentication().getPrincipal();
		Long userAccountId = userDetails.getUserAccountDTO().getUserAccountId();
		list = groupUtilService.findContactByOrgId(orgId,userAccountId);
		return SUCCESS;
	}
	
	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}

	public ContactGroup getContactGroup() {
		return contactGroup;
	}

	public void setContactGroup(ContactGroup contactGroup) {
		this.contactGroup = contactGroup;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Long getGroupType() {
		return groupType;
	}

	public void setGroupType(Long groupType) {
		this.groupType = groupType;
	}

	public int getAllContactNumber() {
		return allContactNumber;
	}

	public void setAllContactNumber(int allContactNumber) {
		this.allContactNumber = allContactNumber;
	}

	public int getRecentContactNumber() {
		return recentContactNumber;
	}

	public void setRecentContactNumber(int recentContactNumber) {
		this.recentContactNumber = recentContactNumber;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getOrganizationDTOs() {
		return organizationDTOs;
	}

	public void setOrganizationDTOs(String organizationDTOs) {
		this.organizationDTOs = organizationDTOs;
	}

	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}
}
